package com.lh.music.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lh.music.PlayApplication;
import com.lh.music.R;
import com.lh.music.constant.Constants;
import com.lh.music.util.TimeHelper;

import java.util.Timer;
import java.util.TimerTask;

import static com.lh.music.R.id.artist_name;
import static com.lh.music.R.id.song_name;
import static com.lh.music.constant.Constants.NEXT_ACTION;
import static com.lh.music.constant.Constants.PREV_ACTION;

public class PlayActivity extends AppCompatActivity implements View.OnClickListener{
    private PlayApplication app;
    private TextView currentTime,sumTime,songName,artistName;
    private SeekBar seekBar;
    private ImageView prevPlay,controlPlay,nextPlay;
    private ImageView icon;
    Timer timer;
    private boolean isChange=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app= (PlayApplication) getApplication();
        setContentView(R.layout.activity_play);
        initView();
        updateView();
        timerUpdateView();
    }
    private Handler hanler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what==1){
                if(app.music!=null){
                    app.currentProgress=app.music.getSongProgress();
                }
                updateView();
            }
        }
    };
    private void timerUpdateView(){
        timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message msg=hanler.obtainMessage();
                msg.what=1;
                hanler.sendMessage(msg);
            }
        },0,200);
    }

    private void updateView() {
        //本地音乐时的更新
        if (app.currentType==Constants.LOCAL){
            if (app.currentSong!=null){
                songName.setText(app.currentSong.getTitle());
                artistName.setText(app.currentSong.getArtist());
                currentTime.setText(TimeHelper.milliSecondsToFormatTimeString(app.currentProgress));
                sumTime.setText(TimeHelper.milliSecondsToFormatTimeString(app.currentDuration));
            }
            seekBar.setMax(app.currentDuration);
            if (!isChange){
                seekBar.setProgress(app.currentProgress);
            }
        }//网络音乐时的更新
        else {
            if (app.currentNetSong!=null&&app.currentNetSong.getBitrate()!=null){
                int duration=app.currentNetSong.getBitrate().getFile_duration()*1000;
                int currentProgress=app.netMusic.getCurrentProgress();
                seekBar.setMax(duration);
                seekBar.setProgress(currentProgress);
                sumTime.setText(TimeHelper.milliSecondsToFormatTimeString(duration));
                currentTime.setText(TimeHelper.milliSecondsToFormatTimeString(currentProgress));
                /*currentTime.setText(TimeHelper.milliSecondsToFormatTimeString(app.currentProgress));
                sumTime.setText(TimeHelper.milliSecondsToFormatTimeString(app.currentDuration));*/
                //排行榜
                if (app.currentNetSong.getSong_id()!=null){
                    songName.setText(app.currentNetSong.getTitle());
                    artistName.setText(app.currentNetSong.getAuthor());
                    Glide.with(this)
                            .load(app.currentNetSong.getPic_big())
                            .placeholder(R.mipmap.a9d)
                            .crossFade()
                            .centerCrop()
                            .into(icon);

                }//电台
                if (app.currentNetSong.getSongid()!=null){
                    songName.setText(app.currentNetSong.getTitle());
                    artistName.setText(app.currentNetSong.getArtist());
                    Glide.with(this)
                            .load(app.currentNetSong.getThumb())
                            .placeholder(R.mipmap.a9d)
                            .crossFade()
                            .centerCrop()
                            .into(icon);
                }
            }
           /* seekBar.setMax(app.currentDuration);
            if (!isChange){
                seekBar.setProgress(app.currentProgress);
            }*/
        }


        switch (app.songState){
            case Constants.PAUSED:
                controlPlay.setImageResource(R.mipmap.play);
                break;
            case Constants.PLAYING:
                controlPlay.setImageResource(R.mipmap.pause);
                break;
        }
    }

    private void initView() {
        currentTime= (TextView) findViewById(R.id.currentTime);
        sumTime= (TextView) findViewById(R.id.sumTime);
        songName= (TextView) findViewById(song_name);
        artistName= (TextView) findViewById(artist_name);
        seekBar= (SeekBar) findViewById(R.id.seekBar);
        prevPlay= (ImageView) findViewById(R.id.prev_play);
        controlPlay= (ImageView) findViewById(R.id.controll_play);
        nextPlay= (ImageView) findViewById(R.id.next_play);
        icon= (ImageView) findViewById(R.id.icon);
        controlPlay.setOnClickListener(this);
        prevPlay.setOnClickListener(this);
        nextPlay.setOnClickListener(this);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isChange=true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                isChange=false;
                app.currentProgress=seekBar.getProgress();
                //发送一个快进或者后退广播
                Intent broadIntent=new Intent();
                broadIntent.setAction(Constants.SEEK_ACTION);
                sendBroadcast(broadIntent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent broadIntent=new Intent();
        switch (v.getId()){
            case  R.id.controll_play:
                //播放类型为本地音乐时发送的广播
                if (app.currentType==Constants.LOCAL){
                    if (app.songState==Constants.PLAYING){
                        //发送一个暂停播放的广播
                        broadIntent.setAction(Constants.PAUSE_ACTION);
                        sendBroadcast(broadIntent);
                        app.songState=Constants.PAUSED;
                    }else if (app.songState==Constants.PAUSED){
                        //发送一个继续播放的广播
                        broadIntent.setAction(Constants.CONTINUE_ACTION);
                        sendBroadcast(broadIntent);
                        app.songState=Constants.PLAYING;
                    }else{
                        //发送一个开始播放的广播
                   /* broadIntent.setAction(Constants.PLAY_ACTION);
                    sendBroadcast(broadIntent);
                    app.songState=Constants.PLAYING;
                    updateMusicView();*/
                        broadIntent.setAction(Constants.PLAY_ACTION);
                        //加载上次播放的列表
                        broadIntent.putExtra("init","init");
                        sendBroadcast(broadIntent);
                        Toast.makeText(app, "初始化音乐，开始播放", Toast.LENGTH_SHORT).show();

                    }
                }//播放类型为网络音乐时发送的广播
                else{
                    if (app.songState==Constants.PLAYING){
                        //发送一个暂停播放的广播
                        broadIntent.setAction(Constants.NET_PAUSE_ACTION);
                        sendBroadcast(broadIntent);
                    }else if (app.songState==Constants.PAUSED){
                        //发送一个继续播放的广播
                        broadIntent.setAction(Constants.NET_CONTINUE_ACTION);
                        sendBroadcast(broadIntent);
                    }
                }

                break;
            case R.id.prev_play:
                //发送一个上一首广播
                //播放状态为本地音乐
                if (app.currentType==Constants.LOCAL){
                    if (app.songState==Constants.STOPED){
                        broadIntent.setAction(Constants.PLAY_ACTION);
                        //加载上次播放的列表
                        broadIntent.putExtra("init","init");
                        sendBroadcast(broadIntent);
                        Toast.makeText(app, "初始化音乐，开始播放", Toast.LENGTH_SHORT).show();
                    }else {
                        broadIntent.setAction(PREV_ACTION);
                        sendBroadcast(broadIntent);
                    }
                }//播放状态为网络音乐
                else {
                   broadIntent.setAction(Constants.NET_PREV_ACTION);
                    sendBroadcast(broadIntent);
                }


                break;
            case R.id.next_play:
                //发送一个上一首广播
                if (app.currentType==Constants.LOCAL){
                    if (app.songState==Constants.STOPED){
                        broadIntent.putExtra("init","init");
                        sendBroadcast(broadIntent);
                        Toast.makeText(app, "初始化音乐，开始播放", Toast.LENGTH_SHORT).show();
                    }else {
                        broadIntent.setAction(NEXT_ACTION);
                        sendBroadcast(broadIntent);
                    }
                }else{
                    broadIntent.setAction(Constants.NET_NEXT_ACTION);
                    sendBroadcast(broadIntent);
                }


                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
